﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetNuke.Entities.Portals;

using Da = VLUNewsData;
using DaMo = VLUNewsData.EntityDataModel;
using System.Collections;

namespace VLUNewsBusiness.VLURolesAndUsers
{
    public class RoleToRole
    {
        #region Base members
        private static RoleToRole _Instance = null;
        /// <summary>
        /// * Get instance of this class.
        /// </summary>
        /// <returns></returns>
        public static RoleToRole Instance()
        {
            if (_Instance == null) _Instance = new RoleToRole();
            return _Instance;
        }
        public static void Release()
        {
            if (_Instance != null)
                GC.SuppressFinalize(_Instance);
        }
        #endregion Base members

        /// <summary>
        /// * Add relationship between two Roles.
        /// </summary>
        /// <param name="pPortalID"></param>
        /// <param name="pFromRoleID"></param>
        /// <param name="pToRoleID"></param>
        /// <param name="pUserID"></param>
        /// <returns>-1: Error; 1: Success; 2: FromRoleID = ToRoleID</returns>
        public int Save(int pPortalID, int pFromRoleID, int pToRoleID, int pUserID)
        {
            int result = -1;
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                bool exist = ent.VluNewsRoleToRoles.Any(p => p.PortalID == pPortalID && p.FromRoleID == pFromRoleID && p.ToRoleID == pToRoleID);
                if (exist)
                    return 2;
                var model = ent.VluNewsRoleToRoles.Where(p => p.FromRoleID == pFromRoleID && p.ToRoleID == pToRoleID).FirstOrDefault();
                if (model == null)
                {
                    ent.VluNewsRoleToRoles.AddObject(new DaMo.VluNewsRoleToRole()
                    {
                        PortalID = pPortalID,
                        FromRoleID = pFromRoleID,
                        ToRoleID = pToRoleID,
                        CreatedByUserID = pUserID,
                        CreatedOnDate = DateTime.Now,
                        LastModifiedByUserID = pUserID,
                        LastModifiedOnDate = DateTime.Now
                    });

                    result = ent.SaveChanges();
                }
            }// using.
            return result;
        }// Add.

        /// <summary>
        /// * Remove relationship between two Roles.
        /// </summary>
        /// <param name="pRoleToRoleID"></param>
        /// <returns></returns>
        public int Delete(int pRoleToRoleID)
        {
            int result = -1;
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                var model = ent.VluNewsRoleToRoles.Where(p => p.RoleToRoleID == pRoleToRoleID).FirstOrDefault();
                if (model != null)
                {
                    ent.VluNewsRoleToRoles.DeleteObject(model);

                    result = ent.SaveChanges();
                }
            }// using.
            return result;
        }// Remove.

        /// <summary>
        /// * Gets all relationship between two roles in the news module in the portal.
        /// </summary>
        /// <param name="pPortalID"></param>
        /// <returns></returns>
        public bool GetExist(int pPortalID, int pFromRoleID, int pToRoleID)
        {
            bool result = false;
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                result = ent.VluNewsRoleToRoles.Any(p => p.PortalID == pPortalID && p.FromRoleID == pFromRoleID && p.ToRoleID == pToRoleID);
            }// using.
            return result;
        }// GetsAll.

        /// <summary>
        /// * Gets all relationship between two roles in the news module in the portal.
        /// </summary>
        /// <param name="pPortalID"></param>
        /// <returns></returns>
        public IList GetsAll(int pPortalID)
        {
            IList result = null;
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                result =
                    (from role in ent.VluNewsRoleToRoles
                     where role.PortalID == pPortalID
                     select new
                     {
                         RoleToRoleID = role.RoleToRoleID,
                         PortalID = role.PortalID,
                         FromRoleID = role.FromRoleID,
                         FromRoleName = role.FromRole.RoleName,
                         ToRoleID = role.ToRoleID,
                         ToRoleName = role.ToRole.RoleName
                     }).ToList();
            }// using.
            return result;
        }// GetsAll.

        /// <summary>
        /// * Gets all roles in the news module in the portal.
        /// </summary>
        /// <param name="pPortalID"></param>
        /// <returns></returns>
        public IList GetsAllRoles(int pPortalID)
        {
            IList result = null;
            int vRoleGroupID = RoleAndUser.GetNewsRoleGroupID();
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                //result = ent.Roles.Select(p => new DaMo.Role() { RoleID = p.RoleID, RoleName = p.RoleName }).ToList();
                result =
                    (from role in ent.Roles
                     where role.PortalID == pPortalID && role.RoleGroupID == vRoleGroupID
                     select new
                     {
                         RoleID = role.RoleID,
                         RoleName = role.RoleName
                     }).ToList();
            }// using.
            return result;
        }// GetsAllRoles.

    }// class.
}// namespace.
